﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
//using System.Xml;
//using System.Xml.Serialization;
using System.IO;
using System.Reflection;

public class GameSaveLoad /*: MonoBehaviour*/ {
/*
	[System.Serializable]
	public struct SerializedUnit {
		public GameSkill skill;
		public int level;
	}
	
	[System.Serializable]
	public struct SerializedPowerUp {
		public GamePowerup powerUp;
		public int level;
	}
	
	[System.Serializable]
	public struct SerializedPool {
		public List<SerializedUnit> serializedUnits = new List<SerializedUnit>();
		public List<SerializedPowerUp> serializedPowerUps = new List<SerializedPowerUp>();
	}
	
	SerializedPool ToSerializedPool(GamePool pool) {
		SerializedPool serializedPool = new SerializedPool();
		//...
	}
	
	void FromSerializedPool(GamePool pool, SerializedPool serializedPool) {
		//...
	}
*/
	static public string getSavePath( string filename ) {
	#if UNITY_IPHONE
		string path = Application.persistentDataPath + "/" + filename;
	#elif UNITY_ANDROID
		string path = Application.persistentDataPath + "/" + filename;
	#else
		string path = Application.dataPath + "/" + filename;
	#endif
		return path;
	}
	
	public static string POOLFILENAME = "GamePool.hex";
	
	//load game pool from file
	static public void LoadPool(out GamePool pool) {
		SetupEnvironment();
		
		pool = null;
		string savePath = getSavePath(POOLFILENAME);
		
		if (File.Exists(savePath)) {
			BinaryFormatter bf = new BinaryFormatter();
			FileStream file = File.Open(savePath, FileMode.Open);
			object obj = bf.Deserialize(file);
			file.Close();
			pool = (GamePool)obj;
			if (pool.teams.Count > 0) {
				GamePool.currentTeam = pool.teams[pool.teamIndex];
			}
		}
	}
	
	//save game pool to file
	static public void SavePool(GamePool pool) {
		SetupEnvironment();
		
		//SerializedPool serializedPool = ToSerializedPool(pool);
		string savePath = getSavePath(POOLFILENAME);
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Create( savePath );
		bf.Serialize(file, pool);
		file.Close();
		
		Debug.Log("GameSaveLoad.SavePool : " + savePath);
	}

	//load game fields from file
	public static string FIELDSFILENAME = "GameField.hex";
	
	static public void LoadFields(out GameFieldSet fields) {
		SetupEnvironment();
		
		string savePath = getSavePath( FIELDSFILENAME );
		if (File.Exists( savePath )) {
			BinaryFormatter bf = new BinaryFormatter();
			FileStream file = File.Open(savePath, FileMode.Open);
			object obj = bf.Deserialize(file);
			file.Close ();
			
			fields = (GameFieldSet)obj;
		}
		else
			fields = new GameFieldSet();

		// default field
		if (fields.fieldIndex < fields.fields.Count) {
			GameFieldSet.activeField = fields.fields[fields.fieldIndex];
		}
		else {
			//create a default fields
			GameFieldSet.activeField = new GameField();
			GameFieldSet.activeField.size = 5;
			fields.fields.Add( GameFieldSet.activeField );
			fields.fieldIndex = 0;
		}
	}
	
	static public void SaveFields( GameFieldSet fields ) {
		SetupEnvironment();
		
		string savePath = getSavePath( FIELDSFILENAME );
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Create( savePath );
		bf.Serialize( file, fields );
		file.Close();
		
		Debug.Log("GameSaveLoad.SavePool : " + savePath);		
	}
			
	static public void SetupEnvironment() {
		System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
	}
}
